package dpreview;

import javax.swing.*;
import java.io.*;

public class FileChooserApp {

	private XMLFilter xmlFilter;
	private JFileChooser fc;

	public FileChooserApp() {
		xmlFilter = new XMLFilter();
		fc = new JFileChooser();
	}

	public File [] openFiles() {

		fc.setDialogTitle("Select File(s)");
		fc.setMultiSelectionEnabled(true);
		
		// Choose only files, not directories
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

		// Start in current directory
		fc.setCurrentDirectory(new File("."));

		// Set filter for XML source files.
		fc.setFileFilter(xmlFilter);
		fc.setAcceptAllFileFilterUsed(false);

		// Now open chooser
		int result = fc.showOpenDialog(null);

		if (result == JFileChooser.CANCEL_OPTION) {
			return null;
		} else if (result == JFileChooser.APPROVE_OPTION) {
			return fc.getSelectedFiles();
		} else {
			return null;
		}
	}

}

/** Filter to work with JFileChooser to select xml file types. **/
class XMLFilter extends javax.swing.filechooser.FileFilter {

	public boolean accept(File f) {
		return f.getName().toLowerCase().endsWith(".xml") || f.isDirectory();
	}

	public String getDescription() {
		return "XML files (*.xml)";
	}
}